package com.zsx.servlet;

import com.zsx.entities.Admin;
import com.zsx.service.admin.ImpAdmin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/admin")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if (type.equals("gly")){
            ImpAdmin impAdmin = new ImpAdmin();
            List<Admin> admins = impAdmin.getAdmin();
            int size = admins.size();
            Integer inputId=Integer.parseInt(req.getParameter("admin"));
            Integer inputPassword = Integer.parseInt(req.getParameter("password"));
            System.out.println("inputId="+inputId);
            System.out.println("inputPassword="+inputPassword);
            System.out.println("admins="+admins);
            System.out.println("size="+size);
            int m=0;
            for(Admin a : admins){
                if (a.getId()==inputId && a.getPassword()==inputPassword){
                    req.setAttribute("thisAdmin",a);
                    System.out.println(a);
                    //转发跳转
                    req.getRequestDispatcher("admin.jsp").forward(req,resp);
                }
                m++;
                if (m==size){
                    resp.sendRedirect("err.jsp");
                }
            }
        }

    }

}
